package array;

import java.util.ArrayList;
import java.util.List;

public class LuckyNumbersInAMatrix1380 {
    public List<Integer> luckyNumbers(int[][] matrix) {
        if (matrix.length == 0) {
            return null;
        }
        int[] xx = new int[matrix.length];
        int[] yy = new int[matrix[0].length];
        for (int i = 0; i < matrix.length; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = 0; j < matrix[0].length; j++) {
                min = Math.min(min,matrix[i][j]);
            }
            xx[i]=min;
        }

        for (int i = 0; i < matrix[0].length; i++) {
            int max = Integer.MIN_VALUE;
            for (int j = 0; j < matrix.length; j++) {
                max = Math.max(max,matrix[j][i]);
            }
            yy[i]=max;
        }

        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < xx.length; i++) {
            for (int j = 0; j < yy.length; j++) {
                if(xx[i]==yy[j]){
                    result.add(xx[i]);
                    continue;
                }
            }
        }
        return result;
    }
}
